package com.template.cms.auth.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Service;

import com.template.auth.mapper.GroupMapper;
import com.template.auth.mapper.RoleMapper;
import com.template.auth.mapper.UserGroupMapper;
import com.template.auth.mapper.UserMapper;
import com.template.auth.mapper.UserRoleMapper;
import com.template.auth.model.Group;
import com.template.auth.model.GroupExample;
import com.template.auth.model.Role;
import com.template.auth.model.RoleExample;
import com.template.auth.model.User;
import com.template.auth.model.UserExample;
import com.template.auth.model.UserGroup;
import com.template.auth.model.UserGroupExample;
import com.template.auth.model.UserGroupExample.Criteria;
import com.template.auth.model.UserRole;
import com.template.auth.model.UserRoleExample;
import com.template.auth.utils.Page;
import com.template.auth.utils.SystemContext;
import com.template.cms.auth.exception.CMSException;

@Service
public class UserService implements IUserService {
	
	@Inject
	private UserMapper userMapper;
	
	@Inject
	private GroupMapper groupMapper;
	
	@Inject
	private RoleMapper roleMapper;
	
	@Inject
	private UserGroupMapper userGroupMapper;
	
	@Inject
	private UserRoleMapper userRoleMapper;

	@Override
	public void add(User user, Integer[] rids, Integer[] gids) {
		User tu = userMapper.selectByPrimaryKey(user.getId());
		if(tu!=null) throw new CMSException("添加用户已存在，不能添加");
		userMapper.insertSelective(user);
		System.out.println("heheheeee");
		//增加用户和角色的关联
		for(Integer rid : rids){
			addUserRole(user, rid);
		}
		//增加用户和组的关联
		for(Integer gid : gids){
			addUserGroup(user, gid);
		}
	}

	private void addUserGroup(User user, Integer gid) {
		Group group = groupMapper.selectByPrimaryKey(gid);
		if(group==null) throw new CMSException("添加的用户组不存在");
		UserGroup userGroup = new UserGroup();
		userGroup.setuId(user.getId());
		userGroup.setgId(gid);
		userGroupMapper.insertSelective(userGroup);
	}

	private void addUserRole(User user, Integer rid) {
		Role role = roleMapper.selectByPrimaryKey(rid);
		if(role==null) throw new CMSException("添加的角色不存在");
		UserRole userRole = new UserRole();
		userRole.setuId(user.getId());
		userRole.setrId(rid);
		userRoleMapper.insertSelective(userRole);
	}

	@Override
	public void delete(int id) {
		//删除所有用户和组的关联关系
		UserGroupExample userGroupExample = new UserGroupExample();
		Criteria userGroupCriteria = userGroupExample.createCriteria();
		userGroupCriteria.andUIdEqualTo(id);
		userGroupMapper.deleteByExample(userGroupExample);
		//删除所有用户和角色的关联关系
		UserRoleExample userRoleExample = new UserRoleExample();
		UserRoleExample.Criteria userRoleCriteria = userRoleExample.createCriteria();
		userRoleCriteria.andUIdEqualTo(id);
		userRoleMapper.deleteByExample(userRoleExample);
		//删除用户
		userMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void update(User user, Integer[] rids, Integer[] gids) {
		//获取已有rids
		UserRoleExample userRoleExample = new UserRoleExample();
		userRoleExample.createCriteria().andUIdEqualTo(user.getId());
		List<UserRole> userRoleList = userRoleMapper.selectByExample(userRoleExample);
		
		List<Integer> roleIdList = new ArrayList<Integer>();
		for(UserRole userRole : userRoleList){
			roleIdList.add(userRole.getrId());
		}
		//获取已有gids
		UserGroupExample userGroupExample = new UserGroupExample();
		userGroupExample.createCriteria().andUIdEqualTo(user.getId());
		List<UserGroup> userGroupList = userGroupMapper.selectByExample(userGroupExample);
		
		List<Integer> groupIdList = new ArrayList<Integer>();
		for(UserGroup userGroup : userGroupList){
			groupIdList.add(userGroup.getgId());
		}
		
		//判断是否有增加rid或gid
		for(Integer rid : rids){
			if(!roleIdList.contains(rid)){
				addUserRole(user, rid);
			}
		}
		for(Integer gid : gids){
			if(!groupIdList.contains(gid)){
				addUserGroup(user, gid);
			}
		}
		
		//判断是否需要删除已有的rid或gid
		for(Integer rid : roleIdList){
			if(!ArrayUtils.contains(rids, rid)){
				UserRoleExample example = new UserRoleExample();
				example.createCriteria().andUIdEqualTo(user.getId()).andRIdEqualTo(rid);
				userRoleMapper.deleteByExample(example);
			}
		}
		for(Integer gid : groupIdList){
			if(!ArrayUtils.contains(gids, gid)){
				UserGroupExample example = new UserGroupExample();
				example.createCriteria().andUIdEqualTo(user.getId()).andGIdEqualTo(gid);
				userGroupMapper.deleteByExample(example);
			}
		}
	}

	@Override
	public void update(User user) {
		userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public void updatePwd(int uid, String oldPwd, String newPwd) {
		
	}

	@Override
	public Page<User> findUser() {
		Integer length = SystemContext.getPageSize();
		Integer begin = SystemContext.getPageOffset();
		String sort = SystemContext.getSort();
		String order = SystemContext.getOrder();
		
		Page<User> page = new Page<User>(begin, length);
		UserExample example = new UserExample();
		example.setOrderByClause(sort + " " + order);
		page.setTotalRecords(userMapper.countByExample(example));
		
		example.setPage(page);
		page.setPageDatas(userMapper.selectByExample(example));
		return page;
	}

	@Override
	public User load(int id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Role> listUserRoles(int id) {
		List<Integer> list = listUserRoleIds(id);
		
		RoleExample roleExample = new RoleExample();
		roleExample.createCriteria().andIdIn(list);
		return roleMapper.selectByExample(roleExample);
	}

	@Override
	public List<Group> listUserGroups(int id) {
		List<Integer> list = listUserGroupIds(id);
		
		GroupExample groupExample = new GroupExample();
		groupExample.createCriteria().andIdIn(list);
		return groupMapper.selectByExample(groupExample);
	}

	@Override
	public List<Integer> listUserRoleIds(int id) {
		UserRoleExample example = new UserRoleExample();
		example.createCriteria().andUIdEqualTo(id);
		List<UserRole> userRoleList = userRoleMapper.selectByExample(example);
		List<Integer> list = new ArrayList<Integer>();
		for(UserRole userRole : userRoleList){
			list.add(userRole.getrId());
		}
		return list;
	}

	@Override
	public List<Integer> listUserGroupIds(int id) {
		UserGroupExample example = new UserGroupExample();
		example.createCriteria().andUIdEqualTo(id);
		List<UserGroup> userGroupList = userGroupMapper.selectByExample(example);
		List<Integer> list = new ArrayList<Integer>();
		for(UserGroup userGroup : userGroupList){
			list.add(userGroup.getgId());
		}
		return list;
	}

	@Override
	public List<User> listGroupUsers(int gid) {
		UserGroupExample example = new UserGroupExample();
		example.createCriteria().andGIdEqualTo(gid);
		List<UserGroup> userGroupList = userGroupMapper.selectByExample(example);
		List<Integer> list = new ArrayList<Integer>();
		for(UserGroup userGroup : userGroupList){
			list.add(userGroup.getuId());
		}
		
		UserExample userExample = new UserExample();
		userExample.createCriteria().andIdIn(list);
		return userMapper.selectByExample(userExample);
	}

	@Override
	public List<User> listRoleUsers(int rid) {
		UserRoleExample example = new UserRoleExample();
		example.createCriteria().andRIdEqualTo(rid);
		List<UserRole> userRoleList = userRoleMapper.selectByExample(example);
		List<Integer> list = new ArrayList<Integer>();
		for(UserRole userRole : userRoleList){
			list.add(userRole.getuId());
		}
		
		UserExample userExample = new UserExample();
		userExample.createCriteria().andIdIn(list);
		return userMapper.selectByExample(userExample);
	}

	@Override
	public User login(String username, String password) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
		List<User> list = userMapper.selectByExample(userExample);
		if(list.size() <= 0){
			throw new CMSException("用户名或密码不正确");
		}
		return list.get(0);
	}

	@Override
	public List<User> listUsers() {
		UserExample userExample = new UserExample();
		userExample.createCriteria();
		return userMapper.selectByExample(userExample);
	}

}
